﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PrintServerTables
{
    /// <summary>
    /// Interaction logic for wndNewServer.xaml
    /// </summary>
    public partial class wndServer : Window
    {
        public Server theServer;
        public MainWindow theParent;
        private string[] sep = new string[1] { "." };
        private bool bUpdate;
        private VisualUtilities visUtil = new VisualUtilities();

        private static List<string> eStrings = new List<string>
        { "Back", "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "Delete", "End", "Home", 
            "Insert", "Left", "NumPad0", "NumPad1", "NumPad2", "NumPad3", "NumPad4", "NumPad5",
            "NumPad6", "NumPad7", "NumPad8", "NumPad9", "Right" };
 
        private static string sWeb = @"^([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$";

        public wndServer()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            bUpdate = false;
            if (theServer != null)
            {
                bUpdate = true;

                if (theServer.privateIp == string.Empty)
                    theServer.privateIp = "0.0.0.0";
                if (theServer.publicIp == string.Empty)
                    theServer.publicIp = "0.0.0.0";
            }
            else
            {
                theServer = new Server();
                theServer.name = theServer.location = theServer.web = "";
                theServer.port = "8000";
                theServer.privateIp = theServer.publicIp = "0.0.0.0";
            }

            tbServerName.Text = theServer.name;
            tbLocation.Text = theServer.location;
            tbWebAddress.Text = theServer.web;
            tbPort.Text = theServer.port;
            SplitIP(theServer.privateIp, true);
            SplitIP(theServer.publicIp, false);
            btnOK.IsEnabled = bUpdate;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string sIPPrivate = GetIPAddress(true);
            string sIPPublic = GetIPAddress(false);

            if ((sIPPrivate == "") && (sIPPublic == "") && (tbWebAddress.Text != ""))
            {
                MessageBox.Show("The server must have at least one of private IP, public IP or web address!", "Create new server", MessageBoxButton.OK, MessageBoxImage.Error);
                btnOK.IsEnabled = false;
                return;
            }

            theServer.name = tbServerName.Text;
            theServer.location = tbLocation.Text;
            theServer.web = tbWebAddress.Text;
            theServer.port = tbPort.Text;
            theServer.privateIp = GetIPAddress(true);
            theServer.publicIp = GetIPAddress(false);
            this.DialogResult = true;
        }

        private void tbIP_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            tb.Background = new SolidColorBrush(Colors.White);
            tb.Foreground = new SolidColorBrush(Colors.DarkBlue);
            if (tb.Text == "")
                tb.Text = "0";

            int ip = Convert.ToInt32(tb.Text);
            tb.Text = ip.ToString();
            tb.SelectionStart = tb.Text.Length;
            if (ip < 256)
            {
                btnOK.IsEnabled = true;
                return;
            }
            tb.Background = new SolidColorBrush(Colors.Red);
            tb.Foreground = new SolidColorBrush(Colors.White);
            btnOK.IsEnabled = false;
        }

        private void tbPort_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            tb.Background = new SolidColorBrush(Colors.White);
            tb.Foreground = new SolidColorBrush(Colors.DarkBlue);
            int ip = Convert.ToInt32(tb.Text);
            if (ip < 65536)
            {
                btnOK.IsEnabled = true;
                return;
            }
            tb.Background = new SolidColorBrush(Colors.Red);
            tb.Foreground = new SolidColorBrush(Colors.White);
            btnOK.IsEnabled = false;
        }

        private void SplitIP(string sIP, bool bPrivate)
        {
            string[] ips = sIP.Split(sep, StringSplitOptions.RemoveEmptyEntries);
            if (bPrivate)
            {
                tbIntraIP1.Text = ips[0];
                tbIntraIP2.Text = ips[1];
                tbIntraIP3.Text = ips[2];
                tbIntraIP4.Text = ips[3];
            }
            else
            {
                tbPublicIP1.Text = ips[0];
                tbPublicIP2.Text = ips[1];
                tbPublicIP3.Text = ips[2];
                tbPublicIP4.Text = ips[3];
            }
        }

        private void tbIP_KeyDown(object sender, KeyEventArgs e)
        {
            if (eStrings.Contains(e.Key.ToString()))
            {
                e.Handled = false;
                return;
            }
            System.Media.SystemSounds.Beep.Play();
            e.Handled = true;
        }

        private void tbWebAddress_LostFocus(object sender, RoutedEventArgs e)
        {
            tbWebAddress.Background = new SolidColorBrush(Colors.White);
            tbWebAddress.Foreground = new SolidColorBrush(Colors.DarkBlue);
            if (tbWebAddress.Text == "")
            {
                btnOK.IsEnabled = true;
                e.Handled = true;
                return;
            }
               
            Regex regex = new Regex(sWeb);
            if (regex.IsMatch(tbWebAddress.Text))
                return;

            tbWebAddress.Background = new SolidColorBrush(Colors.Red);
            tbWebAddress.Foreground = new SolidColorBrush(Colors.White);
            btnOK.IsEnabled = false;
        }

        private void tbServerName_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tbServerName.Text == "")
                btnOK.IsEnabled = false;
            else
                btnOK.IsEnabled = true;
        }

        private void tbServerName_LostFocus(object sender, RoutedEventArgs e)
        {
            tbServerName.Background = new SolidColorBrush(Colors.White);
            tbServerName.Foreground = new SolidColorBrush(Colors.DarkBlue);
            if (tbServerName.Text != "")
            {
                foreach (Server server in theParent.servers)
                {
                    if (server.name == tbServerName.Text)
                    {
                        MessageBox.Show("Duplicate server name!", "Create new server", MessageBoxButton.OK, MessageBoxImage.Error);
                        tbServerName.Background = new SolidColorBrush(Colors.Red);
                        tbServerName.Foreground = new SolidColorBrush(Colors.White);
                        btnOK.IsEnabled = false;
                        return;
                    }
                }
                btnOK.IsEnabled = true;
                e.Handled = true;
                return;
            }

            tbServerName.Background = new SolidColorBrush(Colors.Red);
            tbServerName.Foreground = new SolidColorBrush(Colors.White);
            btnOK.IsEnabled = false;
        }

        private string GetIPAddress(bool bPrivate)
        {
            string sIp = "";

            if (bPrivate)
            {
                sIp = tbIntraIP1.Text + "." + tbIntraIP2.Text + ".";
                sIp = sIp + tbIntraIP3.Text + "." + tbIntraIP4.Text;
                if (sIp == "0.0.0.0")
                    sIp = "";
            }
            else
            {
                sIp = tbPublicIP1.Text + "." + tbPublicIP2.Text + ".";
                sIp = sIp + tbPublicIP3.Text + "." + tbPublicIP4.Text;
                if (sIp == "0.0.0.0")
                    sIp = "";
            }
            return sIp;
        }
    }
}
